import React, { useEffect, useState } from 'react';
import { Breadcrumb } from "antd";
import 'antd/dist/antd.css';
import into from './image/into.png'
import info from './image/info.png'
import column from './image/column.png'
import news from './image/news.jpeg'
import business from './image/business.png'
import party from './image/party.png'
import tcepOfSpiritualLearning from './image/tcepOfSpiritualLearning.jpg'
import team from './image/team.png'
import jt from './image/jt.svg'
import { NavLink, Outlet, useLocation, useNavigate, useSearchParams } from "react-router-dom";
import './index.css'
import $ from "jquery";
import { useAppContext } from "../../App";
import { getTermTranslation, trans } from "../../util/language";

const img = {
    "home": news,
    "into": into,
    "info": info,
    "specialTopicColumn": info,
    "globalSearch": info,
    "column": column,
    "news": news,
    "business": business,
    "party": party,
    "team": team,
    "academic": column,
    "details": news,
    "top20": party,
    "partyDisciplineLearning": party,
    "tcepOfSpiritualLearning": tcepOfSpiritualLearning,
    "education": party
}

let selectLanguage = 'cn';

export const menu = {
    "home": {
        title: "首页",
        children: [
            {
                title: "首页",
                url: "/home"
            },
            {
                title: "首页",
                url: "home"
            },
            {
                title: "详情",
                url: "details"
            }
        ]
    },
    // "details": {
    //     title: "详情",
    //     children: [
    //         {
    //             title: "详情",
    //             url: "details"
    //         }
    //     ]
    // },
    "into": {
        title: "走进总院",
        children: [
            {
                title: "关于总院",
                url: "into",
                params: { docId: 390 }
            }, {
                title: "现任领导",
                url: "leader",
                params: {}
            }, {
                title: "组织机构",
                url: "org",
                params: { docId: 391 }
            }, {
                title: "主要职能",
                url: "function",
                params: { docId: 410 }
            }, {
                title: "总院荣誉",
                url: "honor",
                params: {}
            }
            // , {
            //     title: "网上展厅",
            //     url: "online",
            //     params: {dirId: 289}
            // },
        ]
    },
    "info": {
        title: "信息公开",
        children: [
            {
                title: "基本信息",
                url: "basic",
                params: { dirId: "310,438" }
            }, {
                title: "重大事项",
                url: "major",
                params: { dirId: "231,440" }
            }, {
                title: "通知公告",
                url: "infoNotice",
                params: { dirId: "229,434" }
            }
        ]
    },
    //
    "specialTopicColumn": {
        title: "专题专栏",
        children: [
            {
                title: "基本信息",
                url: "basic",
                params: { dirId: "310,438" }
            }, {
                title: "重大事项",
                url: "major",
                params: { dirId: "231,440" }
            }, {
                title: "通知公告",
                url: "specialTopicColumnNotice",
                params: { dirId: "229,434" }
            }
        ]
    },
    "column": {
        title: "行业管理",
        children: [
            {
                title: "技术标准",
                url: "standards",
                params: { dirId: "912,914,915,917" }
            }, {
                title: "质量监督",
                url: "quality",
                params: { dirId: "918,920,921,923" }
            }, {
                title: "定额管理",
                url: "set",
                params: { dirId: "327,433" }
            }, {
                title: "资格考试",
                url: "exam",
                params: { docId: 327995 }
            }
        ]
    },
    "news": {
        title: "新闻中心",
        children: [
            {
                title: "总院动态",
                url: "dynamic",
                params: { dirId: "256,402" }
            }, {
                title: "水利要闻",
                url: "news",
                params: { dirId: "257,403" }
            }
        ]
    },
    "business": {
        title: "业务范围",
        children: [
            {
                title: "规划编制",
                url: "planning",
                params: { dirId: "295,409" }
            }, {
                title: "战略研究",
                url: "strategy",
                params: { dirId: "296,411" }
            }, {
                title: "技术审查",
                url: "review",
                params: { dirId: "297,413" }
            }, {
                title: "行业管理",
                url: "industry",
                params: { dirId: "298,415" }
            }, {
                title: "市场服务",
                url: "services",
                params: { dirId: "299,417" }
            }, {
                title: "科学研究",
                url: "research",
                params: { dirId: "300,419" }
            }
        ]
    },
    "party": {
        title: "党的建设",
        children: [
            {
                title: "党建动态",
                url: "party",
                params: { dirId: "290,405" }
            }, {
                title: "群团建设",
                url: "build",
                params: { dirId: "291,407" }
            }
        ]
    },
    "team": {
        title: "人才团队",
        children: [
            {
                title: "人才队伍",
                url: "team",
                params: { docId: 392 }
            },
            {
                title: '人才创新团队',
                url: "innovationTeam",
                params: { docId: 351158 }
            },
            {
                title: '揭榜挂帅团队',
                url: "teamnew",
                params: { docId: 351160 }
            },
            // {
            //     title: '团队建设',
            //     url: "teamBuilder",
            //     params: {docId: 286080}
            // },

            {
                title: "招贤纳士",
                url: "call",
                params: { dirId: "303,421" }
            }
        ]
    },
    "academic": {
        title: "社团组织",
        children: [
            {
                title: "社团组织",
                url: "academic",
                params: { docId: 356967 }
            }
        ]
    },
    "top20": {
        title: "二十大专栏",
        children: [
            {
                title: "党中央决策部署",
                url: "top20",
                params: { dirId: "441,443" }
            }, {
                title: "部党组工作要求",
                url: "jobRequirement",
                params: { dirId: "444,446" }
            }, {
                title: "总院学习贯彻",
                url: "headquartersStudyAndImplementation",
                params: { dirId: "447,449" }
            }, {
                title: "学习参考",
                url: "learningReference",
                params: { dirId: "450,452" }
            }
        ]
    },
    "partyDisciplineLearning": {
        title: "党纪学习教育",
        children: [
            {
                title: "上级部署要求",
                url: "partyDisciplineLearning",
                params: { dirId: "749,750" }
            }, {
                title: "院党委落实",
                url: "partyCommitteeImplementation",
                params: { dirId: "751,752" }
            }, {
                title: "基层党组织落实",
                url: "grassrootsImplementation",
                params: { dirId: "753,754" }
            }, {
                title: "学习参考",
                url: "partyLearningReference",
                params: { dirId: "755,756" }
            }
        ]
    },
    "tcepOfSpiritualLearning": {
        title: "深入贯彻中央八项规定精神学习教育",
        children: [
            {
                title: "上级部署要求",
                url: "tcepOfSpiritualLearning",
                params: { dirId: "930,932" }
            }, {
                title: "院党委落实",
                url: "tcepImplementationByThePartyCommitteeOfTheHospital",
                params: { dirId: "933,935" }
            }, {
                title: "基层党组织落实",
                url: "tcepGrassRootsPartyOrganizationsImplement",
                params: { dirId: "936,938" }
            }, {
                title: "学习参考",
                url: "theCentralEightProvisionsLearningReference",
                params: { dirId: "939,941" }
            }
        ]
    },
    "education": {
        title: "主题教育专栏",
        children: [
            {
                title: "中央部署要求",
                url: "education",
                params: { dirId: "529,534" }
            }, {
                title: "部党组部署要求",
                url: "organizationRequireMent",
                params: { dirId: "530,536" }
            }, {
                title: "院党委落实",
                url: "organizationAndImplementation",
                params: { dirId: "531,538" }
            }, {
                title: "基层党组织落实",
                url: "primaryOrganization",
                params: { dirId: "679,681" }
            }, {
                title: "学习参考",
                url: "learningReference2",
                params: { dirId: "532,540" }
            }, {
                title: "文件要求",
                url: "fileSpecification",
                params: { dirId: "541,543" }
            }
        ]
    }
}

export function getDocId(url, page) {

    // Normalize URL by removing leading/trailing slashes
    const normalizedUrl = url

    // Extract the last segment of the URL
    const urlSegments = normalizedUrl.split('/');
    const lastSegment = urlSegments[urlSegments.length - 1];

    // Find matching menu item
    debugger
    for (const menuKey in innerMenu) {
        const getDocMenu = innerMenu[menuKey];
        const childrenList = getDocMenu.children
        // if (childrenList && childrenList.length >0 ) {
        for (const child of childrenList.children) {
            // Check if URL or page matches the child's URL
            if (child.url === lastSegment || child.url === page) {
                let dirId = child.params && child.params.docId ? String(child.params.docId) : ''
                console.log(dirId)
                return dirId
            }
        }
        // }
    }

    // Return empty string if no match is found
    // Return docId if it exists, otherwise return empty string
    return '';
}

export const innerMenu = {
    "home": {
        title: getTermTranslation(selectLanguage, "首页"),
        children: [
            {
                title: getTermTranslation(selectLanguage, "首页"),
                url: "/home"
            },
            {
                title: getTermTranslation(selectLanguage, "详情"),
                url: "home"
            },
            {
                title: getTermTranslation(selectLanguage, "详情"),
                url: "details"
            }
        ]
    },
    // "details": {
    //     title: "详情",
    //     children: [
    //         {
    //             title: "详情",
    //             url: "details"
    //         }
    //     ]
    // },
    "into": {
        title: getTermTranslation(selectLanguage, "走进总院"),
        children: [
            {
                title: getTermTranslation(selectLanguage, "关于总院"),
                url: "into",
                params: { docId: 390 }
            }, {
                title: getTermTranslation(selectLanguage, "现任领导"),
                url: "leader",
                params: {}
            }, {
                title: getTermTranslation(selectLanguage, "组织机构"),
                url: "org",
                params: { docId: 391 }
            }, {
                title: getTermTranslation(selectLanguage, "主要职能"),
                url: "function",
                params: { docId: 410 }
            }, {
                title: getTermTranslation(selectLanguage, "总院荣誉"),
                url: "honor",
                params: {}
            }, {
                title: getTermTranslation(selectLanguage, "联系我们"),
                url: "contactUsPage",
                params: {}
            }
            // , {
            //     title: "网上展厅",
            //     url: "online",
            //     params: {dirId: 289}
            // },
        ]
    },
    "info": {
        title: getTermTranslation(selectLanguage, '信息公开'),
        children: [
            {
                title: getTermTranslation(selectLanguage, '基本信息'),
                url: "basic",
                params: { dirId: "310,438" }
            }, {
                title: getTermTranslation(selectLanguage, '重大事项'),
                url: "major",
                params: { dirId: "231,440" }
            }, {
                title: getTermTranslation(selectLanguage, '通知公告'),
                url: "infoNotice",
                params: { dirId: "229,434" }
            }
        ]
    },
    "specialTopicColumn": {
        title: "专题专栏",
        children: [
            {
                title: "南水北调",
                url: "diversion",
                params: { dirId: "423,304" }
            }, {
                title: "水网建设",
                url: "waterNetWorkBuild",
                params: { dirId: "427,307" }
            }
            , {
                title: "水美乡村",
                url: "village",
                params: { dirId: "429,308" }
            }
            , {
                title: "河流复苏",
                url: "revival",
                params: { dirId: "316,431" }
            }, {
                title: "安全生产",
                url: "production",
                params: { dirId: "675,677" }
            }, {
                title: "二十大专栏",
                url: "twentyCongress",
                params: { dirId: "441,443,444,446,447,449,450,452" }
            }, {
                title: "党纪学习教育",
                url: "dutyEducation",
                params: { dirId: "749,750,751,752,753,754,755,756" }
            }, {
                title: "深入贯彻中央八项规定精神学习教育",
                url: "eightRules",
                params: { dirId: "930,932,933,935,936,938,939,941" }
            }, {
                title: "主题教育专栏",
                url: "themeEducation",
                params: { dirId: "529,534,530,536,531,538,679,681,532,540,541,543" }
            }
        ]
    },
    "column": {
        title: getTermTranslation(selectLanguage, '行业管理'),
        children: [
            {
                title: getTermTranslation(selectLanguage, '技术标准'),
                url: "standards",
                params: { dirId: "912,914,915,917" }
            }, {
                title: getTermTranslation(selectLanguage, '质量监督'),
                url: "quality",
                params: { dirId: "918,920,921,923" }
            }, {
                title: getTermTranslation(selectLanguage, '定额管理'),
                url: "set",
                params: { dirId: "327,433" }
            },
            {
                title: getTermTranslation(selectLanguage, "资格考试"),
                url: "exam",
                params: { docId: 327995 }
            },
            {
                title: getTermTranslation(selectLanguage, '社团组织'),
                url: "academic",
                params: { docId: 356967 }
            },
            /*
            {
                title: getTermTranslation(selectLanguage,'政策办法'),
                url: "standards_func",
                params: {
                    dirId: '912,914'
                }
            },
            {
                title: getTermTranslation(selectLanguage,'标准编制'),
                url: "standards_set",
                params: {
                    dirId: '915,917'
                }
            },
            {
                title: getTermTranslation(selectLanguage,'监督动态'),
                url: "quality_dync",
                params: {
                    dirId: '921,923'
                }
            },
            {
                title: getTermTranslation(selectLanguage,'政策办法'),
                url: "quality_func",
                params: {
                    dirId: '918,920'
                }
            },

             */
        ]
    },
    "news": {
        title: getTermTranslation(selectLanguage, '新闻中心'),
        children: [
            {
                title: getTermTranslation(selectLanguage, '总院动态'),
                url: "dynamic",
                params: { dirId: "256,402" }
            }, {
                title: getTermTranslation(selectLanguage, '水利要闻'),
                url: "news",
                params: { dirId: "257,403" }
            }, {
                title: getTermTranslation(selectLanguage, '通知公告'),
                url: "newsNotice", //
                params: { dirId: "229,434" }
            }
        ]
    },
    "business": {
        title: getTermTranslation(selectLanguage, '业务范围'),
        children: [
            {
                title: getTermTranslation(selectLanguage, '规划编制'),
                url: "planning",
                params: { dirId: "295,409" }
            }, {
                title: getTermTranslation(selectLanguage, '战略研究'),
                url: "strategy",
                params: { dirId: "296,411" }
            }, {
                title: getTermTranslation(selectLanguage, '技术审查'),
                url: "review",
                params: { dirId: "297,413" }
            },
            {
                title: getTermTranslation(selectLanguage, '市场服务'),
                url: "services",
                params: { dirId: "299,417" }
            }, {
                title: getTermTranslation(selectLanguage, '科学研究'),
                url: "research",
                params: { dirId: "300,419" }
            }
        ]
    },
    "party": {
        title: getTermTranslation(selectLanguage, '党的建设'),
        children: [
            {
                title: getTermTranslation(selectLanguage, '党建动态'),
                url: "party",
                params: { dirId: "290,405" }
            }, {
                title: getTermTranslation(selectLanguage, '群团建设'),
                url: "build",
                params: { dirId: "291,407" }
            }
        ]
    },
    "team": {
        title: getTermTranslation(selectLanguage, '人才团队'),
        children: [
            {
                title: getTermTranslation(selectLanguage, '人才队伍'),
                url: "team",
                params: { docId: 392 }
            },
            {
                title: getTermTranslation(selectLanguage, '人才创新团队'),
                url: "innovationTeam",
                params: { docId: 286080 }
            },
            {
                title: getTermTranslation(selectLanguage, '揭榜挂帅团队'),
                url: "teamnew",
                params: { docId: 286080 }
            },
            // {
            //     title: getTermTranslation(selectLanguage,'团队建设'),
            //     url: "teamBuilder",
            //     params: {docId: 286080}
            // },
            {
                title: getTermTranslation(selectLanguage, '招贤纳士'),
                url: "call",
                params: { dirId: "303,421" }
            }
        ]
    },
    "top20": {
        title: getTermTranslation(selectLanguage, '二十大专栏'),
        children: [
            {
                title: getTermTranslation(selectLanguage, '党中央决策部署'),
                url: "top20",
                params: { dirId: "441,443" }
            }, {
                title: getTermTranslation(selectLanguage, '部党组工作要求'),
                url: "jobRequirement",
                params: { dirId: "444,446" }
            }, {
                title: getTermTranslation(selectLanguage, '总院学习贯彻'),
                url: "headquartersStudyAndImplementation",
                params: { dirId: "447,449" }
            }, {
                title: getTermTranslation(selectLanguage, '学习参考'),
                url: "learningReference",
                params: { dirId: "450,452" }
            }
        ]
    },
    "partyDisciplineLearning": {
        title: getTermTranslation(selectLanguage, '党纪学习教育'),
        children: [
            {
                title: getTermTranslation(selectLanguage, '上级部署要求'),
                url: "partyDisciplineLearning",
                params: { dirId: "749,750" }
            }, {
                title: getTermTranslation(selectLanguage, '院党委落实'),
                url: "partyCommitteeImplementation",
                params: { dirId: "751,752" }
            }, {
                title: getTermTranslation(selectLanguage, '基层党组织落实'),
                url: "grassrootsImplementation",
                params: { dirId: "753,754" }
            }, {
                title: getTermTranslation(selectLanguage, '学习参考'),
                url: "partyLearningReference",
                params: { dirId: "755,756" }
            }
        ]
    },
    "tcepOfSpiritualLearning": {
        title: getTermTranslation(selectLanguage, '深入贯彻中央八项规定精神学习教育'),
        children: [
            {
                title: getTermTranslation(selectLanguage, '上级部署要求'),
                url: "tcepOfSpiritualLearning",
                params: { dirId: "930,932" }
            }, {
                title: getTermTranslation(selectLanguage, '院党委落实'),
                url: "tcepImplementationByThePartyCommitteeOfTheHospital",
                params: { dirId: "933,935" }
            }, {
                title: getTermTranslation(selectLanguage, '基层党组织落实'),
                url: "tcepGrassRootsPartyOrganizationsImplement",
                params: { dirId: "936,938" }
            }, {
                title: getTermTranslation(selectLanguage, '学习参考'),
                url: "theCentralEightProvisionsLearningReference",
                params: { dirId: "939,941" }
            }
        ]
    },
    "education": {
        title: getTermTranslation(selectLanguage, '主题教育专栏'),
        children: [
            {
                title: getTermTranslation(selectLanguage, '中央部署要求'),
                url: "education",
                params: { dirId: "529,534" }
            }, {
                title: getTermTranslation(selectLanguage, '部党组部署要求'),
                url: "organizationRequireMent",
                params: { dirId: "530,536" }
            }, {
                title: getTermTranslation(selectLanguage, '院党委落实'),
                url: "organizationAndImplementation",
                params: { dirId: "531,538" }
            }, {
                title: getTermTranslation(selectLanguage, '基层党组织落实'),
                url: "primaryOrganization",
                params: { dirId: "679,681" }
            }, {
                title: getTermTranslation(selectLanguage, '学习参考'),
                url: "learningReference2",
                params: { dirId: "532,540" }
            }, {
                title: getTermTranslation(selectLanguage, '文件要求'),
                url: "fileSpecification",
                params: { dirId: "541,543" }
            }
        ]
    },
    "globalSearch": {
        title: getTermTranslation(selectLanguage, '全站搜索'),
        children: [
            {
                title: getTermTranslation(selectLanguage, '全站搜索'),
                url: "globalSearch",
                params: { dirId: "930,932" }
            }
        ]
    },

}

function getMenuInfo(currMenu) {
    if (currMenu === 'portal') {
        return {
            title: "首页",
            url: "/home",
            params: {},
        }
    }
    const flatMap = Object.values(menu).flatMap(item =>
        item.children.map(child => ({
            title: child.title, // 移除全部的 /
            url: child.url === '/home' ? 'home' : child.url,
            params: child.params || {}
        }))
    );
    const find = flatMap.find(item => item.url === currMenu);
    if (find !== undefined) {
        return find;
    } else {
        return {
            title: "undefined",
            url: "/home",
            params: {},
        }
    }
}
let canNavigate = false;
function Portal() {
    const {
        selectTab,
        setSelectTab,
        selectLanguage,
        setSelectLanguage,
        routeHistory,
        setRouteHistory,
    } = useAppContext();


    const [width, setWidth] = useState(1920)
    const [height, setHeight] = useState(280)

    const location = useLocation();
    let navigate = useNavigate();
    const { pathname } = location
    const state = location.state || {}
    let { page, text, params } = state
    if (!page) {
        const pages = [
            // "details",
            "into",
            'notice',
            "info",
            "team",
            "news",
            "business",
            "column",
            "academic",
            "home",
            "party",
            "top20",
            "partyDisciplineLearning",
            "tcepOfSpiritualLearning",
            "education"
        ]

        for (const tmp of pages) {
            // menu -> innerMenu
            const currMenu = innerMenu[tmp]
            if (currMenu && currMenu.children) {

                currMenu.children.map(item => {
                    if (pathname.indexOf(item.url) > 0) {
                        page = tmp
                        text = item.title
                        params = item.params
                        canNavigate = true
                        setSelectTab('')
                    }
                })
            }


        }
    } else {
        canNavigate = false
    }
    const [search] = useSearchParams();
    const hiddenLeft = search.get("hiddenLeft") || 0;
    useEffect(() => {
        calcProportion()
        window.addEventListener("resize", calcProportion)
        if (canNavigate) {
            canNavigate = false
            if (state.selectYears) {
                navigate(
                    pathname,
                    { state: { page, text, params, selectYears: state.selectYears } })
            } else {
                navigate(pathname, { state: { page, text, params } })
            }
        }
    }, [])

    function calcProportion() {
        const _root = $('#root')
        let img_width = _root.width();
        let img_height = 280 * img_width / 1920;
        setWidth(img_width)
        setHeight(img_height)
    }

    function getToUrl() {
        const { children } = menu[page]
        let url = "";
        children.map(item => {
            if (item.title === text) {
                url = item.url
            }
        })
        return url;
    }
    function findMenuByUrl(menu, url) {
        // debugger
        for (const key in menu) {
            const item = menu[key];
            // 检查当前项的 children 中是否有匹配的 url
            const foundChild = item.children.find(child => child.url === url);
            if (foundChild) {
                // 返回当前项的结构，但只包含匹配的子项
                return {
                    [key]: {
                        title: item.title,
                        children: [foundChild]
                    }
                };
            }
        }
        return null; // 如果没有找到，返回 null
    }





    useEffect(() => {
        // 每次路由变化时，将当前路径添加到历史记录
        setRouteHistory((prev) => [...prev, location.pathname]);

    }, [location]);

    function breadcrumbList() {
        const pathNameList = pathname.split("/");
        let lastPathName = pathNameList[pathNameList.length - 1];

        let secondName = ''
        let secondUrl = ''

        if (selectTab === '总院动态') {
            secondName = getTermTranslation(selectLanguage, '总院动态')
            secondUrl = 'dynamic'
        } else if (selectTab === '水利要闻') {
            secondName = getTermTranslation(selectLanguage, '水利要闻')
            secondUrl = 'news'
        } else if (selectTab === '通知公告') {
            secondName = getTermTranslation(selectLanguage, '通知公告')
            secondUrl = 'newsNotice'
        } else if (selectTab === '专题专栏') {
            secondName = getTermTranslation(selectLanguage, '专题专栏')
            secondUrl = lastPathName
        }

        let secondBreadCrumb = null;
        let firstBreadCrumb = null;
        if (secondName !== '') {
            // 通过二级找一级的
            const result = findMenuByUrl(innerMenu, secondUrl);
            const menuKey = Object.keys(result)[0]; // "education"
            const rootName = result[menuKey].title;
            const children = result[menuKey].children[0]
            firstBreadCrumb = (
                <Breadcrumb.Item>
                    <NavLink className="portal-breadcrumb-content-one"
                        to={'/'}
                        state={{
                            page,
                            text: rootName,
                            params: {}
                        }}
                    >
                        {
                            (secondName === '专题专栏' && lastPathName === 'details') ? '专题专栏' : rootName
                        }
                    </NavLink>
                </Breadcrumb.Item>
            )
            // 分两种情况
            // 1. 点击专题专栏 更多的时候， 正常显示二级菜单
            // 2. 点击 专题专栏 栏目下的 文章列表， 二级菜单指定 新闻详情
            debugger
            if (secondName == '专题专栏' && lastPathName == 'details') {
                secondBreadCrumb = (
                    <Breadcrumb.Item>
                        <NavLink className="portal-breadcrumb-content-one"
                            to={children.url}
                            state={{
                                page,
                                // text: secondName !== '' ? secondName : children.title,
                                text: '新闻',//children.title,
                                params: children.params
                            }}
                        >
                            {
                                // secondName !== '' ? secondName : children.title
                                '新闻详情'
                            }
                        </NavLink>
                    </Breadcrumb.Item>
                )
            } else {
                secondBreadCrumb = (
                    <Breadcrumb.Item>
                        <NavLink className="portal-breadcrumb-content-one"
                            to={children.url}
                            state={{
                                page,
                                // text: secondName !== '' ? secondName : children.title,
                                text: children.title,
                                params: children.params
                            }}
                        >
                            {
                                // secondName !== '' ? secondName : children.title
                                children.title
                            }
                        </NavLink>
                    </Breadcrumb.Item>
                )
            }

            return [firstBreadCrumb, secondBreadCrumb]
        }

        // 使用示例 第一个参数 menu ->  innerMenu
        // 对于 details 特殊处理

        if (lastPathName === 'details') {
            if (routeHistory.length >= 2) {
                const routeHistoryElement = routeHistory[routeHistory.length - 2];
                const newPathNameList = routeHistoryElement.split("/");

                lastPathName = newPathNameList[newPathNameList.length - 1];
            }
        }
        // debugger
        const result = findMenuByUrl(innerMenu, lastPathName);
        const menuKey = Object.keys(result)[0]; // "education"
        const rootName = result[menuKey].title;
        const children = result[menuKey].children[0]

        debugger
        const rootCrumb = (
            <Breadcrumb.Item>
                <NavLink className="portal-breadcrumb-content-one"
                    to={'/'}
                    state={{
                        page,
                        text: rootName,
                        params: {}
                    }}
                >
                    {rootName}
                </NavLink>
            </Breadcrumb.Item>
        )
        // debugger
        // 详情分为两种情况 detail -> 总院动态 和 水利要闻
        // 二级标签本身就禁止点击
        const childrenCrumb = (
            <Breadcrumb.Item>
                <NavLink className="portal-breadcrumb-content-one"
                    to={children.url}
                    state={{
                        page,
                        // text: secondName !== '' ? secondName : children.title,
                        text: children.title,
                        params: children.params
                    }}
                >
                    {
                        // secondName !== '' ? secondName : children.title
                        children.title
                    }
                </NavLink>
            </Breadcrumb.Item>
        )
        // 对于三级的特殊处理吧


        if (rootName === '社团组织' || rootName === 'AcademicOrganization') {
            return [childrenCrumb]
        }

        return [rootCrumb, childrenCrumb]
    }

    function getTitle() {
        // debugger
        const innerMenu1 = innerMenu[page];
        if (innerMenu1) {
            return state.selectYears ? state.selectYears + innerMenu1.title : innerMenu1.title
        }
        return ''
    }

    function toPage(url, page, text, params, event) {
        // debugger
        if (url !== '/home') {
            setSelectTab('')
        }

        navigate(url, {
            replace: false,
            state: {
                params,
                page,
                text
            }
        })
        if (event != null) {
            event.stopPropagation();
        }
    }

    return (
        <div className="portal-main">
            <div className="portal-header" style={{ height: height }}>
                <img src={
                    img[page]
                } alt="" style={{ width: width, height: height }} />
                <div className="portal-breadcrumb">
                    <Breadcrumb className="portal-breadcrumb-content" separator=">">
                        {/*<Breadcrumb.Item>*/}
                        {/*    <HomeOutlined className="portal-breadcrumb-content-one-icon"*/}
                        {/*                  onClick={() => toPage("/home", "")}/>*/}
                        {/*<span className="portal-breadcrumb-content-one"*/}
                        {/*      onClick={() => toPage("/home", "")}>首页</span>*/}
                        {/*</Breadcrumb.Item>*/}

                        {
                            breadcrumbList()
                        }


                        {/*{*/}
                        {/*    page === "home" || page === "academic" || page === "top20" || page === "partyDisciplineLearning" || page === "tcepOfSpiritualLearning" || page==="education" ? null : (*/}
                        {/*        <Breadcrumb.Item>*/}
                        {/*            <NavLink className="portal-breadcrumb-content-one"*/}
                        {/*                     to={getToUrl()}*/}
                        {/*                     state={{page, text: text, params: params}}*/}
                        {/*            >{text + '2'}</NavLink>*/}
                        {/*        </Breadcrumb.Item>*/}
                        {/*    )*/}
                        {/*}*/}
                    </Breadcrumb>
                </div>

            </div>
            <div className="portal-content">
                {
                    hiddenLeft === "1" || page === "home" || page === "academic" ? (
                        <div className="portal-content-center">
                            <div className="portal-content-main">
                                {/*<div className="portal-content-main-title">*/}
                                {/*    {text || menu[page].title}*/}
                                {/*</div>*/}
                                <Outlet />
                            </div>
                        </div>
                    ) : (
                        <div className="portal-content-center">
                            <div className="portal-content-left">
                                <div className="portal-menu-title">{getTitle()}</div>
                                <div className="portal-menu-separated" />
                                {innerMenu[page].children &&
                                    innerMenu[page].children.map((item, index) => (
                                        <MenuItem
                                            key={index}
                                            item={item}
                                            index={index}
                                            page={page}
                                            state={state}
                                            selectLanguage={selectLanguage}
                                            toPage={toPage}
                                            jt={jt}
                                        />
                                    ))}
                            </div>
                            <div className="portal-content-right">
                                <div className="portal-content-right-main">
                                    <div className="portal-content-right-main-title">
                                        {/*{text}*/}
                                    </div>
                                    <Outlet />
                                </div>
                            </div>
                        </div>
                    )
                }
            </div>
        </div>
    );
}

const MenuItem = ({ item, index, page, state, selectLanguage, toPage, jt }) => {
    const [isHovered, setIsHovered] = useState(false);

    return (
        <div
            className="portal-menu-container"
            onMouseEnter={() => {
                if (item.title === '技术标准' || item.title === '质量监督') {
                    setIsHovered(true);
                }
            }}
            onMouseLeave={() => setIsHovered(false)}
        >
            <NavLink
                className="portal-menu-one"
                to={item.url}
                state={{
                    page,
                    text: item.title,
                    params: item.params,
                    selectYears: state.selectYears,
                }}
            >
                <div className="portal-menu-one-text">{item.title}</div>
                <img src={jt} alt="图标" className="portal-menu-one-icon" />
            </NavLink>

            {/* 二级菜单 */}
            {isHovered && (
                <div className="submenu">
                    {item.title === '技术标准' && (
                        <>
                            <div
                                className="submenu-item"
                                onClick={(event) =>
                                    toPage(
                                        '/portal/standards',
                                        'column',
                                        getTermTranslation(selectLanguage, '政策办法'),
                                        {
                                            dirId: '912,914'
                                        },
                                        event
                                    )
                                }
                            >
                                {getTermTranslation(selectLanguage, '政策办法')}
                            </div>
                            <div
                                className="submenu-item"
                                onClick={(event) =>
                                    toPage(
                                        '/portal/standards',
                                        'column',
                                        getTermTranslation(selectLanguage, '标准编制'),
                                        { dirId: '915,917' },
                                        event
                                    )
                                }
                            >
                                {getTermTranslation(selectLanguage, '标准编制')}
                            </div>
                        </>
                    )}
                    {item.title === '质量监督' && (
                        <>
                            <div
                                className="submenu-item"
                                onClick={(event) =>
                                    toPage(
                                        '/portal/quality',
                                        'column',
                                        getTermTranslation(selectLanguage, '政策办法'),
                                        { dirId: '918,920' },
                                        event
                                    )
                                }
                            >
                                {getTermTranslation(selectLanguage, '政策办法')}
                            </div>
                            <div
                                className="submenu-item"
                                onClick={(event) =>
                                    toPage(
                                        '/portal/quality',
                                        'column',
                                        getTermTranslation(selectLanguage, '监督动态'),
                                        { dirId: '921,923' },
                                        event
                                    )
                                }
                            >
                                {getTermTranslation(selectLanguage, '监督动态')}
                            </div>
                        </>
                    )}
                </div>
            )}
        </div>
    );
};

export default Portal;
